﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using JYB.SummitExam.IDAL;
namespace JYB.SummitExam.DAL
{
    public class SystemJurisdictionDAL : IJurisdictionDAL
    {
        public static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();

        #region 根据角色编号获取对应角色的权限
        List<T> IJurisdictionDAL.GetJurisdictionList<T>(int id)
        {
            string sql = "select {0} from [{1}] where RoleId=@id ";
            Type type = typeof(T);
            var SelectField = string.Join(",", type.GetProperties().Select(p => string.Format("{0}", p.Name)));
            sql = string.Format(sql, SelectField, type.Name);
            List<T> list = new List<T>();
            using (SqlConnection con = new SqlConnection(connString))
            {
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                com.Parameters.Add(new SqlParameter("@id", id));
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    var oObject = (T)Activator.CreateInstance(type);
                    foreach (var item in type.GetProperties())
                    {
                        object oValue = reader[item.Name];
                        if (oValue is DBNull)
                        {
                            item.SetValue(oObject, null);
                        }
                        else
                        {
                            item.SetValue(oObject, oValue);
                        }
                    }
                    list.Add(oObject);
                }
            }
            return list;
        }
        #endregion
    }
}
